package vn.vfriends.id.jpa.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.*;
import javax.validation.constraints.Size;
import org.eclipse.persistence.annotations.ConversionValue;
import org.eclipse.persistence.annotations.Convert;
import org.eclipse.persistence.annotations.ObjectTypeConverter;

/**
 *
 * @author tuan@vfriends.vn
 */
@Entity
@Table(name="SYS_GROUP")
@NamedQueries({
    @NamedQuery(name = "Group.findByName", query = "SELECT g FROM Group g WHERE g.name = :name"),
    @NamedQuery(name = "Group.findByCode", query = "SELECT g FROM Group g WHERE g.code = :code")})
public class Group implements Serializable{

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(nullable = false)
    private Long id;
    
    @Size(min = 1, max = 50)
    @Column(name = "NAME", unique=true, length=50)
    private String name;
    
    @Size(max = 100)
    @Column(length=100)
    private String description;
    
    /* Group is owner */
    @ManyToMany(mappedBy="groups")
    private List<User> users = new ArrayList<User>();
    
    @ManyToMany(cascade= CascadeType.MERGE)
    @JoinTable(name = "GROUP_ROLE", joinColumns = @JoinColumn(name = "GROUP_ID"), 
            inverseJoinColumns = @JoinColumn(name = "ROLE_ID"))
    private List<Role> roles = new ArrayList<Role>();

    public enum GroupCode {
        ADMINISTRATOR("ADMIN"), CUSTOMER("CUS");
        
        private String code;
        
        private GroupCode(String code) {
            this.code = code;
        }

        public String getCode() {
            return code;
        }
        
        @Override
        public String toString() {
            return code;
        }
    }
    
    @ObjectTypeConverter(name = "groupCodeConverter", objectType = GroupCode.class, dataType = String.class, conversionValues = {
        @ConversionValue(objectValue = "ADMINISTRATOR", dataValue = "ADMIN"),
        @ConversionValue(objectValue = "CUSTOMER", dataValue = "CUS")})
    
    @Convert("groupCodeConverter")
    @Column(unique=true, length=20)
    private GroupCode code;
    
    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<User> getUsers() {
        return users;
    }

    public void setUsers(List<User> users) {
        this.users = users;
    }

    public List<Role> getRoles() {
        return roles;
    }

    public void setRoles(List<Role> roles) {
        this.roles = roles;
    }

    public GroupCode getCode() {
        return code;
    }

    public void setCode(GroupCode code) {
        this.code = code;
    }


    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Group other = (Group) obj;
        if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 41 * hash + (this.id != null ? this.id.hashCode() : 0);
        return hash;
    }
}
